package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.SearchEntBean;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class JoinTeamVM extends TitleVM {
    public JoinTeamVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("申请加入");

    }

    public ObservableField<SearchEntBean.DataDTO> entity = new ObservableField<>();
    public ObservableField<String> name =new ObservableField<>();
    public ObservableField<String> reason =new ObservableField<>();

    public BindingCommand postData= new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (ValidateUtils.isEmptyString(name.get())){
                ToastUtils.showShort("请输入姓名");
                return;
            }
            if (ValidateUtils.isEmptyString(reason.get())){
                ToastUtils.showShort("请输入申请理由");
                return;
            }

            saveData();
        }
    });

    private void saveData() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("companyid",entity.get().getId()+"");
        map.put("name",name.get());
        map.put("reason",reason.get());
        model.addbroker(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult o) {
                        if (o.issucc){
                            ToastUtils.showShort("申请成功");
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
